clear
set more off
global DFAD 	"/Mac/DFAD"
global dofiles  "$DFAD/DFAD-DOFILES"
global input 	"$DFAD/DFAD-INPUT"
global output 	"$DFAD/DFAD-OUTPUT"
global paper 	"$DFAD/DFAD-PAPER/Latex"
cd "$DFAD"

*** Purely cosmetic -- sets global macros that define colors in graphs below
	do "/Mac/Macros/JCS Graph Colors.do"
	mycolors white

*********************************
*** Assemble DHS and WDI data ***
*********************************

*** Reshape DHS data	
	use "$output/JDS/primary-secondary-DHS-140710.dta", clear

	replace indicator = subinstr(indicator,"-","",.)
	split byvariable, g(subgroup)
	replace subgroup1 = lower(substr(subgroup1,1,1))
	replace indicator = substr(indicator,1,4)
	replace indicator = "0610" if indicator=="610"
	replace indicator = "0615" if indicator=="615"
	gen		myindicator = "a" + subgroup1 + "er_" + indicator if topic=="School attendance "
	replace	myindicator = "p" + subgroup1 + "er_" + indicator if topic=="School attendance ratios: Primary school"
	replace	myindicator = "s" + subgroup1 + "er_" + indicator if topic=="School attendance ratios: Secondary school"
	
	keep country year africa myindicator value
	ren value  dhs_ 
	reshape wide dhs_, i(country year africa) j(myindicator) string
	tempfile dhs
	save `dhs', replace
		
*** Download WDI data: warning, slow
	/*
	foreach var in SE.PRM.AGES SE.PRM.DURS SE.PRM.ENRR SE.PRM.ENRR.FE SE.PRM.ENRR.MA SE.PRM.NENR SE.PRM.NENR.FE SE.PRM.NENR.MA SE.PRM.PRIV.ZS SE.SEC.AGES SE.SEC.DURS SE.SEC.ENRR SE.SEC.ENRR.FE SE.SEC.ENRR.MA SE.SEC.NENR SE.SEC.NENR.FE SE.SEC.NENR.MA SE.SEC.PRIV.ZS{
		di in green "`var'"
		qui{
				clear
			wbopendata, indicator(`var')
				rename 	countryname country
				split 	indicatorname, p(".") g(temp)
				capture replace temp3 = temp3 + temp4
				gen 	varname = "wdi_" + substr(temp2,1,1) + temp3
			reshape long yr, i(country) j(year)
				local 	varname = varname
				rename 	yr `varname'
				keep 	country year `varname'
				tempfile `varname' 
				save 	``varname'', replace
			mmerge 	country year using `wdi_PAGES'
				drop 	_merge
			save `wdi_PAGES', replace
		}
	}
	save "$output/wdi_education.dta", replace
	*/
	
*** Merge WDI data into DHS data
	use `dhs', clear
	replace country = "Congo, Rep." if country == "Congo (Brazzaville)"
	replace country = "Egypt, Arab Rep." if country=="Egypt"
	mmerge country year using "$output/wdi_education.dta"
	
*** DHS has 2 series - combine
	local	bound = 10
	gen 	dhs_pner 		= dhs_pner_Tota
	replace dhs_pner 		= dhs_ater_06`bound' if dhs_pner==.
	gen		dhs_type_pner 	= 0 if dhs_pner == dhs_pner_Tota
	replace	dhs_type_pner 	= 1 if dhs_pner == dhs_ater_06`bound'
	gen 	dhs_sner 		= dhs_sner_Tota
	replace dhs_sner 		= dhs_ater_1115 if dhs_sner==.
	gen		dhs_type_sner 	= 0 if dhs_sner == dhs_sner_Tota
	replace	dhs_type_sner 	= 1 if dhs_sner == dhs_ater_1115	


****************
*** Pictures ***
****************

*** Kenya
	preserve
		keep if country=="Kenya"
		global N = _N + 1
		set obs $N
		gen		hhner	= 71.2 if year==1997
		replace hhner	= 71.6 if year==2006
		sort 	year
		tsset 	year
		gen		mymarker = "MOE" 	if year==1998
		replace	mymarker = "KNBS" 	if year==1997
		replace	mymarker = "DHS" 	if year==2003
		twoway	(tsline dhs_pner_Tota 			if year>1995 & year<2010, lcolor($c1))							///
				(tsline hhner		 			if year>1995 & year<2010, lcolor($c3))							///
				(tsline wdi_PNENR 				if year>1995 & year<2010, lcolor($c2))							///
				(scatter dhs_pner_Tota 	year	if year>1995 & year<2010, mcolor($c1))							///
				(scatter hhner		 	year	if year>1995 & year<2010, mcolor($c3))							///
				(scatter wdi_PNENR 		year	if year>1995 & year<2010, mcolor($c2))							///
				(scatter dhs_pner_Tota 	year	if year>2002 & year<2004, mcolor($c1) mlabel(mymarker) mlabcolor($c1) mlabpos(10))	///
				(scatter hhner		 	year	if year>1996 & year<1998, mcolor($c3) mlabel(mymarker) mlabcolor($c3) mlabpos(10))  ///
				(scatter wdi_PNENR 		year	if year>1997 & year<1999, mcolor($c2) mlabel(mymarker) mlabcolor($c2) mlabpos(10)),	///
				xlabel(, $myxlabel) 																			///
				ylabel(, $myylabel) 																			///
				xscale(/*range()*/ lcolor($cf))																	///
				yscale(/*range()*/ noline)																		///
				xtitle("", color($cf)) 																			///
				$myregion 																						///
				ytitle("Net primary enrollment (%)", color($cf))												///
				legend(/*pos(10) ring(0) lab(5 "NRVA 2007/08") lab(6 "AMS 2010") order(5 6) $mylegend*/ off) 	///
				saving("$output/tsline_kenya_ed.gph", replace)
				*gr export "$paper/tsline_kenya_ed.pdf", as(pdf) replace	
	restore

*** Rwanda
	preserve
		keep if country=="Rwanda"
		global N = _N + 2
		set obs $N	
		gen		hhner	= 86.6 if year==2005
		replace hhner	= 91.7 if year==2010
		sort 	year
		tsset 	year
		gen		mymarker = "MINEDUC" if year==2001
		replace	mymarker = "NISR" 	if year==2005
		replace	mymarker = "DHS" 	if year==2000
		twoway 	(tsline wdi_PNENR 				if year>1998 & year<2011, lcolor($c2))							///
				(tsline dhs_pner_Tota 			if year>1998 & year<2011, lcolor($c3))							///
				(tsline hhner		 			if year>1998 & year<2011, lcolor($c1))							///
				(scatter wdi_PNENR 		year	if year>1998 & year<2011, mcolor($c2))							///
				(scatter dhs_pner_Tota 	year	if year>1998 & year<2011, mcolor($c3))							///
				(scatter hhner		 	year	if year>1998 & year<2011, mcolor($c1))							///
				(scatter wdi_PNENR 		year	if year>2000 & year<2002, mcolor($c2) mlabel(mymarker) mlabcolor($c2) mlabpos(10))	///
				(scatter dhs_pner_Tota 	year	if year>1999 & year<2001, mcolor($c3) mlabel(mymarker) mlabcolor($c3) mlabpos(10))	///
				(scatter hhner		 	year	if year>2004 & year<2006, mcolor($c1) mlabel(mymarker) mlabcolor($c1) mlabpos(10)), ///
				xlabel(, $myxlabel) 																			///
				ylabel(, $myylabel) 																			///
				xscale(/*range()*/ lcolor($cf))																	///
				yscale(/*range()*/ noline)																		///
				xtitle("", color($cf)) 																			///
				$myregion 																						///
				ytitle("Net primary enrollment", color($cf))													///
				legend(/*pos(10) ring(0) lab(5 "NRVA 2007/08") lab(6 "AMS 2010") order(5 6) $mylegend*/ off) 	///
				saving("$output/tsline_rwanda_ed.gph", replace)
				*gr export "$paper/tsline_rwanda_ed.pdf", as(pdf) replace	
	restore
	drop if wdi_PNENR==.
	keep if _merge==3
	drop _merge



************************
*** Create FPE dummy ***
************************

		replace country = trim(subinstr(country,",","",.))
		gen 	fpe = 0 if africa==1
		gen		fpedate = 2007 if country=="Burkina Faso" 	
		replace	fpedate = 2005 if country=="Eritrea" 		 // at latest
		replace	fpedate = 2002 if country=="Ethiopia" 		
		replace	fpedate = 2003 if country=="Kenya" 			
		replace	fpedate = 2013 if country=="Namibia" 		
		replace	fpedate = 2009 if country=="Niger" 			 // at latest
		replace	fpedate = 1999 if country=="Nigeria" 		
		replace	fpedate = 2003 if country=="Rwanda" 		
		replace	fpedate = 2001 if country=="Senegal" 		
		replace	fpedate = 2001 if country=="Tanzania" 		
		replace	fpedate = 1994 if country=="Malawi" 		
		replace	fpedate = 1997 if country=="Uganda" 		
		replace	fpedate = 2002 if country=="Zambia" 		
		replace	fpedate = 2010 if country=="Swaziland" 		
		replace	fpedate = 2006 if country=="Benin"		 	
		replace	fpedate = 1999 if country=="Cameroon"	 	
		replace	fpedate = 2005 if country=="Ghana" 			
		replace	fpedate = 2000 if country=="Lesotho"	 	
		replace	fpedate = 2006 if country=="Burundi"	 	
		replace	fpedate = 1997 if country=="Central African Republic"	 	
		replace	fpedate = 2007 if country=="Congo, Rep."	 	
		replace	fpedate = 1996 if country=="Gabon"	 	
		replace	fpedate = 2002 if country=="Madagascar"	 	
		replace	fpedate = 2004 if country=="Mozambique"	 	
		replace	fpedate = 2001 if country=="Sierra Leone"	 	
		replace	fpedate = 2008 if country=="Togo"	 	
         
		replace fpe = 1 if year>=fpedate
		sort 	country year
		by country: gen Dfpe = fpe - fpe[_n-1]
		
*** Changes
	foreach var in year dhs_pner dhs_sner dhs_type_pner dhs_type_sner wdi_PNENR wdi_SNENR wdi_PENRR wdi_SENRR{
		sort country year
		by country: gen lag`var' = `var'[_n-1]
		by country: gen fwd`var' = `var'[_n+1]
	}
	foreach var in year dhs_pner dhs_sner dhs_type_pner dhs_type_sner wdi_PNENR wdi_SNENR wdi_PENRR wdi_SENRR{
		gen D`var' 		= (`var' - lag`var')
		gen D10`var' 	= 10*(`var' - lag`var')/(year - lagyear)
		gen gr_`var' 	= 100*((`var'/lag`var')^(1/(year-lagyear)) - 1)
		gen fgr_`var' 	= 100*((fwd`var'/`var')^(1/(fwdyear-year)) - 1)
	}

*******************************
*** Regression table (loop) ***
*******************************

	rename 	wdi_PNENR wdi_pnenr
	rename 	wdi_SNENR wdi_snenr
	rename 	Dwdi_PNENR Dwdi_pnenr
	rename 	Dwdi_SNENR Dwdi_snenr
	gen 	fpetemp = fpe
	gen 	dhs = .
	gen 	wdi = .
	label variable dhs "DHS enrollment (\%)"
	label variable fpe "Free primary educ (dummy)"
	label variable year "Time trend"
	replace year = year-2000
	encode country, g(ccode)
	

	foreach l in p {

		replace dhs = dhs_`l'ner
		replace wdi = wdi_`l'nenr
		gen 	type = dhs_type_`l'ner	
		reg dhs fpe year type if africa==1,  cluster(ccode)
			est2vec educ_140503`l', replace vars(fpe year dhs _cons) name(DHSOLS)
			replace dhs = Ddhs_`l'ner
			replace fpe = Dfpe
			replace type = Ddhs_type_`l'ner
		reg dhs fpe year type if africa==1,  cluster(ccode)
			est2vec educ_140503`l', addto(educ_140503`l') name(DHSFE)
			replace fpe = fpetemp
			replace type = dhs_type_`l'ner
		reg wdi fpe year type if africa==1, cluster(ccode)
			est2vec educ_140503`l', addto(educ_140503`l') name(WDIOLS)
			replace wdi = Dwdi_`l'nenr
			replace fpe = Dfpe
			replace type = Ddhs_type_`l'ner
		reg wdi fpe year type if africa==1, cluster(ccode)
			est2vec educ_140503`l', addto(educ_140503`l') name(WDIFE)
			replace fpe = fpetemp
			replace wdi = wdi_`l'nenr
			replace dhs = dhs_`l'ner
			replace type = dhs_type_`l'ner
		reg wdi dhs fpe year type if africa==1,  cluster(ccode)
			est2vec educ_140503`l', addto(educ_140503`l') name(GAPOLS)
			replace fpe = Dfpe
			replace wdi = Dwdi_`l'nenr
			replace dhs = Ddhs_type_`l'ner
			replace type = Ddhs_type_`l'ner
		reg wdi dhs fpe year type if africa==1,  cluster(ccode)
			est2vec educ_140503`l', addto(educ_140503`l') name(GAPFE)
			est2tex educ_140503`l', preserve path("$paper") levels (90 95 99) extracol(2 4) fancy label replace digits(2) mark(stars)
	}
	

***************************
*** Summary stats table ***
***************************

		replace year = year + 2000
		reg Dwdi_pnenr  Ddhs_pner
		keep if e(sample)
		count
		gen discrepancy = Dwdi_pnenr  - Ddhs_pner
		format *pnenr *pner discrep %4.1f
		forval n = 1/13{
			gen and`n' = "&"
		}
		gen break = "\\"
		replace country = subinstr(country,",","",.)
		count
		gsort -discrep
		keep if africa==1
		order country and1 lagyear and2 year and3 and4 lagwdi_PNENR and5 wdi_pnenr and6 Dwdi_pnenr and7 and8 lagdhs_pner and9 dhs_pner and10 Ddhs_pner and11 and12 discrep and13 fpedate break
		outsheet country and1 lagyear and2 year and3 and4 lagwdi_PNENR and5 wdi_pnenr and6 Dwdi_pnenr and7 and8 lagdhs_pner and9 dhs_pner and10 Ddhs_pner and11 and12 discrep and13 fpedate break using "$paper/all_ed_changes.csv", comma replace
exit
